<?php

class Admin_GaleriaController extends App_Controller_Action
{
    public $dir = 'public/upload/galeria/';
    
    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction() {
        // variaveis de sessão para multiupload
        $_SESSION['dir_url'] = $_SERVER['DOCUMENT_ROOT'].$this->_urlbase.'/';
        $_SESSION['dir_uploud'] = $_SESSION['dir_url'].$this->dir;
        $_SESSION['img_width'] = 196;
        $_SESSION['img_height'] = 196;
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/s_galeria.js');
        
        Zend_Loader::loadClass('imagem','library');
        
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            //if(is_uploaded_file($_FILES['imagem'])) {
                
                $id = (int) $this->_getParam('id',0);
                
                if($this->_getParam('tamanho') == 'N')
                    $returnImg = imagem::upload('imagem',$this->dir,$id,TRUE,196,196);
                else
                    $returnImg = imagem::upload('imagem',$this->dir,$id,TRUE,394,394);
                
                $data = array(
                    'GAL_ID'    => $id,
                    'GAL_NOM'   => $this->_getParam('titulo'),
                    'GAL_ORD'   => $this->_getParam('tamanho'),
                );
                $obj = new Admin_Model_DbTable_Galeria();

                if ($id == 0){
                    $obj->insert($data);
                    $id =  $obj->getAdapter()->lastInsertId();                    
                    imagem::rename($id,$this->dir,$returnImg['filenew'],TRUE);
                }
           //} 
        }
        
        $obj = new Admin_Model_DbTable_Galeria();
        $this->view->result = $obj->fetchAll($obj->select()->order('GAL_ORD ASC'));
        
    }
    
    
    public function editAction() {
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender(); 
        $html = '';
        $id = '';
        
        if($this->getRequest()->isPost()) {
            
            $id = (int) $this->_getParam('id');
            //$tam = $this->_getParam('tamanho') == 'N' ? 'D':'N'; // inverte
            /*    
            $data = array(
                'GAL_ID'    => $id,
                //'GAL_NOM'   => $this->_getParam('titulo'),
                'GAL_TAM'   => $tam,
            );
           
            
            $obj = new Admin_Model_DbTable_Galeria();
            $obj->update($data, 'GAL_ID = ' . $id);
             */
            
            Zend_Loader::loadClass('imagem','library');
            
            foreach(glob($this->dir.$id.'_tb.*') as $file) $filenewp = $file;
            
            foreach(glob($this->dir.$id.'.*') as $file) $filename = $file;
            
            $tam_img = getimagesize($filenewp);
            $tam = $tam_img[0];
            
            if($tam == '196')
                imagem::reize($filename,$filenewp, 394, 394);
            else
                imagem::reize($filename,$filenewp,196,196);
            
            //$row = $obj->fetchRow('GAL_ID = ' . $id);
            
            $html =  $this->view->partial('galeria/img.phtml', array('urlbase' => $this->_urlbase, 'controller' => $this->_controller,'id'=>$id));           

            
        }else {           
            
            //$id = $this->_getParam('id');

            //$obj = new Admin_Model_DbTable_Galeria();
            //$result = $obj->fetchRow('GAL_ID = '.$id);

           /// $html =  $this->view->partial('galeria/edit.phtml', array('urlbase' => $this->_urlbase, 'controller' => $this->_controller,'result'=>$result));           

        }
        
        echo json_encode(array('ok' => true, 'html' => $html, 'id' => $id));
    
    }

    public function deleteAction() {
        
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        
        $ok = FALSE;
        
        $id = $this->_getParam('id', 0);
        $src = $this->_getParam('src', '');
        if ((int)$id > 0 ) {
            
            $obj = new Admin_Model_DbTable_Galeria();
            $ok = (bool) $obj->delete('GAL_ID = '.(int)$id);
            
            $ok = (bool) $obj->delete('GAL_NOM like "'.$src.'"');
            
            Zend_Loader::loadClass('imagem','library');
            imagem::delete($id,$this->dir,TRUE);
        }
        
        echo json_encode(array('ok' => TRUE));
    }
    public function ordemAction() {
        
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        
        $id = (int) $this->_getParam('id',0);
        $ord = (int) $this->_getParam('ordem',0);
        
        foreach(glob($this->dir.$id.'_tb.*') as $file) $img = $file;
        
        $data = array(
            'GAL_ID'    => $id,
            'GAL_NOM'   => $img,
            'GAL_ORD'   => $ord,
        );
        
        $where = 'GAL_ID = '.$id;
        
        $obj = new Admin_Model_DbTable_Galeria();
        $row = $obj->fetchRow($where);
        
        if(isset($row->GAL_ID) && $row->GAL_ID > 0)
        {
            $obj->update($data,'GAL_ID = '.$id);
        }else{
            $obj->insert($data);
        }
                
        echo json_encode(array('ok' => TRUE));
    }
    
}

